package aigle.bvieillard.gmf.tp2.diagram.navigator;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import aigle.bvieillard.gmf.tp2.diagram.edit.parts.ComposeEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.ComposeQuantiteEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.IngredientEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.IngredientNameEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.LivreRecettesEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.LocalisationEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.LocalisationNameEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.RecetteEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.RecetteNomEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.RecetteOrigineRegionaleEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.VinEditPart;
import aigle.bvieillard.gmf.tp2.diagram.edit.parts.VinNomEditPart;
import aigle.bvieillard.gmf.tp2.diagram.part.Tp2DiagramEditorPlugin;
import aigle.bvieillard.gmf.tp2.diagram.part.Tp2VisualIDRegistry;
import aigle.bvieillard.gmf.tp2.diagram.providers.Tp2ElementTypes;
import aigle.bvieillard.gmf.tp2.diagram.providers.Tp2ParserProvider;

/**
 * @generated
 */
public class Tp2NavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		Tp2DiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		Tp2DiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof Tp2NavigatorItem
				&& !isOwnView(((Tp2NavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof Tp2NavigatorGroup) {
			Tp2NavigatorGroup group = (Tp2NavigatorGroup) element;
			return Tp2DiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof Tp2NavigatorItem) {
			Tp2NavigatorItem navigatorItem = (Tp2NavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (Tp2VisualIDRegistry.getVisualID(view)) {
		case LivreRecettesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http:///recette1-tp2-v0?LivreRecettes", Tp2ElementTypes.LivreRecettes_1000); //$NON-NLS-1$
		case RecetteOrigineRegionaleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http:///recette1-tp2-v0?Recette?origineRegionale", Tp2ElementTypes.RecetteOrigineRegionale_4002); //$NON-NLS-1$
		case VinEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///recette1-tp2-v0?Vin", Tp2ElementTypes.Vin_2004); //$NON-NLS-1$
		case LocalisationEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///recette1-tp2-v0?Localisation", Tp2ElementTypes.Localisation_2001); //$NON-NLS-1$
		case RecetteEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///recette1-tp2-v0?Recette", Tp2ElementTypes.Recette_2002); //$NON-NLS-1$
		case ComposeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http:///recette1-tp2-v0?Compose", Tp2ElementTypes.Compose_4001); //$NON-NLS-1$
		case IngredientEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http:///recette1-tp2-v0?Ingredient", Tp2ElementTypes.Ingredient_2003); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = Tp2DiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& Tp2ElementTypes.isKnownElementType(elementType)) {
			image = Tp2ElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof Tp2NavigatorGroup) {
			Tp2NavigatorGroup group = (Tp2NavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof Tp2NavigatorItem) {
			Tp2NavigatorItem navigatorItem = (Tp2NavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (Tp2VisualIDRegistry.getVisualID(view)) {
		case LivreRecettesEditPart.VISUAL_ID:
			return getLivreRecettes_1000Text(view);
		case RecetteOrigineRegionaleEditPart.VISUAL_ID:
			return getRecetteOrigineRegionale_4002Text(view);
		case VinEditPart.VISUAL_ID:
			return getVin_2004Text(view);
		case LocalisationEditPart.VISUAL_ID:
			return getLocalisation_2001Text(view);
		case RecetteEditPart.VISUAL_ID:
			return getRecette_2002Text(view);
		case ComposeEditPart.VISUAL_ID:
			return getCompose_4001Text(view);
		case IngredientEditPart.VISUAL_ID:
			return getIngredient_2003Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getIngredient_2003Text(View view) {
		IParser parser = Tp2ParserProvider.getParser(
				Tp2ElementTypes.Ingredient_2003,
				view.getElement() != null ? view.getElement() : view,
				Tp2VisualIDRegistry.getType(IngredientNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			Tp2DiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRecette_2002Text(View view) {
		IParser parser = Tp2ParserProvider.getParser(
				Tp2ElementTypes.Recette_2002,
				view.getElement() != null ? view.getElement() : view,
				Tp2VisualIDRegistry.getType(RecetteNomEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			Tp2DiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getVin_2004Text(View view) {
		IParser parser = Tp2ParserProvider.getParser(Tp2ElementTypes.Vin_2004,
				view.getElement() != null ? view.getElement() : view,
				Tp2VisualIDRegistry.getType(VinNomEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			Tp2DiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getLocalisation_2001Text(View view) {
		IParser parser = Tp2ParserProvider
				.getParser(Tp2ElementTypes.Localisation_2001,
						view.getElement() != null ? view.getElement() : view,
						Tp2VisualIDRegistry
								.getType(LocalisationNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			Tp2DiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getCompose_4001Text(View view) {
		IParser parser = Tp2ParserProvider.getParser(
				Tp2ElementTypes.Compose_4001,
				view.getElement() != null ? view.getElement() : view,
				Tp2VisualIDRegistry.getType(ComposeQuantiteEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			Tp2DiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRecetteOrigineRegionale_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getLivreRecettes_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return LivreRecettesEditPart.MODEL_ID.equals(Tp2VisualIDRegistry
				.getModelID(view));
	}

}
